//
//  setMailVC.m
//  tcpoctest
//
//  Created by apple on 13-3-21.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "setMailVC.h"
#import "RegexKitLite.h"
#import "taskDataSource.h"

@implementation setMailVC

- (id)init
{
    self = [super init];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"设置邮箱";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    //[backItem release];
    
    UIBarButtonItem *modifyItem = [self createBarButtonItemWithTitle:@"提交" target:self action:@selector(modifyButtonPressed)];
    self.navItem.rightBarButtonItem = modifyItem;
    //[modifyItem release];
    
    UIImageView *inputBgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 55, 300, 44)];
    inputBgView.image = [UIImage imageNamed:@"cell.png"];
    inputBgView.userInteractionEnabled = YES;
    [self.view addSubview:inputBgView];
    //[inputBgView release];
    
    inputTF = [[UITextField alloc]initWithFrame:CGRectMake(5, 0, 290, 44)];
    inputTF.font = CENTER_CELL_CONTENT_FONT;
    inputTF.textColor=[UIColor colorWithWhite:0.4 alpha:1.0];
    inputTF.borderStyle=UITextBorderStyleNone;
    inputTF.leftViewMode = UITextFieldViewModeAlways;
    inputTF.placeholder = @"请输入邮箱";
    inputTF.text = [DataControl shareControl].personDS.uinfo.mymail;
    inputTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    inputTF.delegate =self;
    [inputBgView addSubview:inputTF];
    //[inputTF release];
}

#pragma mark -private fun-
-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
}

-(void)modifyButtonPressed
{
    if ([inputTF isFirstResponder])
    {
        [inputTF resignFirstResponder];
    }
    
    if ([self CheckInput])
    {
        [DataControl shareControl].personDS.uinfo.mymail = inputTF.text;
        
        [[DataControl shareControl] SetUpdateUserType:UUT_SELFMAIL];
        [[DataControl shareControl].LCDS addLogicCommand:LC_UPDATEUIDB];
        
        [[DataControl shareControl].LCDS addLogicCommand:LC_SETMAIL];
        
        [[ViewManager sharedViewManager] backView:self];
        
        [[DataControl shareControl].taskDS CompleteTaskByID:TI_F_CHANGEMAIL
         withIsDirectShow:true];
        
    }
}

-(bool)CheckInput
{
    bool bright = true;
    
    do
    {
        if ([inputTF.text length] == 0)
        {
            [[ViewManager sharedViewManager] showFailHUDWithStatus:@"请输入邮箱"
                                                         withDelay:2
                                                          withView:self.view];
            bright = false;
            break;
        }
        
        if (![inputTF.text isMatchedByRegex:@"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b"])
        {
            [[ViewManager sharedViewManager] showFailHUDWithStatus:@"邮箱格式不对耶"
                                                         withDelay:2
                                                          withView:self.view];
            bright = false;
            break;
        }
        
    }while (false);
    
    return bright;
}

#pragma mark -textfield delegate-
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    [self modifyButtonPressed];
    
    return true;
}

@end
